/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2017年11月22日, 下午5:57
 */

#include <cstdlib>
using namespace std;
#include"PublicTool.h"
#include"CSwitchManager.h"
#include"Common.h"
#include"PublicLog.h"
#include<signal.h>
#include"PublicMySql.h"
#include"time.h"
#include"CSwitchGui.h"
#define SOFT_VERSION "0.0.12.19"
void ShowVersion(){
	printf("VERSION:" SOFT_VERSION "\n");
	printf("GCC:" __VERSION__ "\n");
	printf("BUILD:" __DATE__ " " __TIME__ "\n");
	exit(0);
}
void ShowHelp(){
	INFO("-h 显示帮助");
	INFO("-f 后台运行");
	INFO("-once 运行一次");
	INFO("-flow 带宽采集(存到RRD)");
	INFO("-conf 显示配置");
	INFO("-tool 附加工具");
	INFO("-init 初始化数据库表");
	exit(0);
}
mysysconf g_sysconf={"public",1,0,0,0,"",0};
static void Catcher(int sig){
	g_sysconf.running=0;
}
int  LoadConfigFile();
void ShowConfig();
void InitNetSnmp();
void InitMySQLDb();
void RunForkThread();
void RunWorkThread();
void tool(int argc, char** argv);
void test(int argc, char** argv);
int main(int argc, char** argv){
	InitWorkPath();
	if(LoadConfigFile()!=0){
		return -1;
	}
	g_sysconf.threads=sysconf(_SC_NPROCESSORS_ONLN);
	if(g_sysconf.threads<8){
		g_sysconf.threads=8;
	}
	int bFork=0;
	int i=0;
	for(i=1;i<argc;i++){
		//防止退出
		if(strcmp(argv[i],"-null")==0){
			
		}else
		if(strcmp(argv[i],"-tool")==0){
			tool(argc-i,argv+i);
			exit(0);
		}else
		if(strcmp(argv[i],"-test")==0){
			test(argc-i,argv+i);
			exit(0);
		}else
		if(strcmp(argv[i],"-f")==0||strcmp(argv[i],"-fork")==0){
			bFork=1;
		}else
		if(strcmp(argv[i],"-debug")==0){
			SetEnableDebug(1);
		}else
		if(strcmp(argv[i],"-once")==0){
			g_sysconf.once=1;
		}else
		if(strcmp(argv[i],"-flow")==0){
			g_sysconf.flow=1;
		}else
		if(strcmp(argv[i],"-init")==0){
			InitMySQLDb();
			exit(0);
		}else
		if(strcmp(argv[i],"-conf")==0){
			ShowConfig();
			exit(0);
		}else
#if USE_GTK_WINDOW			
		if(strcmp(argv[i],"-gui")==0){
			CSwitchGui win;
			win.RunMain();
			exit(0);
		}else
#endif			
		if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"-help")==0){
			ShowHelp();
		}else
		if(strcmp(argv[i],"-v")==0||strcmp(argv[i],"-version")==0){
			ShowVersion();
		}else{
			ShowHelp();
		}
	}
	signal(SIGINT,Catcher);
	signal(SIGTERM,Catcher);
	if(bFork){
		RunForkThread();
	}else{
		OpenPublicLog("switch_collect");
		InitNetSnmp();
		sock_startup();
		RunWorkThread();
		sock_cleanup();
		ClosePublicLog();
	}
	
#if 0	
	CSwitchManager switchmanager;
	switchmanager.UpdateSwitchIPLIST();
	switchmanager.UpdateSwitchSysInfo();
	switchmanager.UpdateSwitchifTable();
#endif
#if 0	
	CSwitchifTable switchbase("171.8.79.1");
	switchbase.UpDateifTable();
#endif	

	return 0;
}

void RunForkThread(){
	InitFork();
	int nPid=0;
	while(g_sysconf.running){
		nPid=CheckChildFork(nPid,g_sysconf.flow==1?"-flow":"-null",NULL);
		sleep(5);
	}
#ifndef __MINGW32__	
	if(nPid>0)kill(nPid,SIGINT);
#endif	
}
void RunWorkThread(){
	time_t checktime=0;
	while(g_sysconf.running){
		time_t now=time(NULL);
		if(now/300!=checktime/300){
			checktime=now;
			CSwitchManager switchmanager;
			//更新IP列表
			switchmanager.UpdateSwitchIPLIST();
			//获取sys信息同时排除不通的
			switchmanager.UpdateSwitchSysInfo();
			//获取网卡信息
			if(g_sysconf.flow){
			//只跟端口表有关	
				switchmanager.UpdateSwitchifTable();
			}else{
			//除端口表外其它信息采集	
				switchmanager.UpdateSwitchDataTable();
			}
			if(g_sysconf.once){
				g_sysconf.running=0;
			}
		}else{
			sleep(5);
		}
	}
}